package Pages;

import java.util.Date;
import java.util.HashMap;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;

import clientMain.*;

/**
 * Register User Page
 * @author Andrew Fox
 *
 */

public class RegisterUserPage extends Page {
	HashMap<String, StringBuffer> data=new HashMap<String, StringBuffer>();

	public RegisterUserPage(Page page){
		//		super(runSettings.appURL+"/registeruser",page.lastURL,page.client);
		//		this.html=page.html;
		//		this.pageType=page.pageType;
		super(page.url,page.html,page.client,page.pageType);
		this.pageOpenTime=page.pageOpenTime;
		this.lastURL=page.lastURL;
	}

	/**
	 * Determines the next link to go to in the application
	 * @return the URL or HTML of the next link, depending on whether
	 * 		   the previous entry contained a form or not
	 */
	public StringBuffer makeDecision() throws Exception{
		StringBuffer newPage=new StringBuffer();
		String threeDigits=Long.toString(Math.round(Math.random()*999));
		boolean success=false;
		JsonNode node = null;
		if (HTML4==false){
			ObjectMapper mapper = new ObjectMapper();
			if(verbose)System.out.println(html);
			node = mapper.readValue(html.toString(), JsonNode.class);
			success=node.get("registeruser").getBooleanValue();

		}
		if(HTML4==true){
			data=getFormData();
			if (html.indexOf("The username is already taken")!=-1)
				client.userInfo.username=new StringBuffer(client.userInfo.username).append(new StringBuffer("_").append(threeDigits));
			if (html.indexOf("The email address is already taken")!=-1){
				int atSign=client.userInfo.email.indexOf("@");
				client.userInfo.email=new StringBuffer(client.userInfo.email).insert(atSign,new StringBuffer("_").append(threeDigits));
			}


			data.put("username",typingError(client.userInfo.username));
			data.put("password1",typingError(client.userInfo.password));
			data.put("password2",typingError(client.userInfo.password));
			data.put("email1",typingError(client.userInfo.email));
			data.put("email2",typingError(client.userInfo.email));
			data.put("firstname",typingError(client.userInfo.firstName));
			data.put("lastname",typingError(client.userInfo.lastName));
			data.put("street",typingError(client.userInfo.address));
			data.put("town",typingError(client.userInfo.city));
			data.put("zip",typingError(client.userInfo.zipcode));
			data.put("state",typingError(client.userInfo.USStateCode));
		}
		else if(HTML4==false&&success==false){
			data.put("username",typingError(client.userInfo.username));
			data.put("password",typingError(client.userInfo.password));
			data.put("email",typingError(client.userInfo.email));
			data.put("firstname",typingError(client.userInfo.firstName));
			data.put("lastname",typingError(client.userInfo.lastName));
		}
		else{// HTML5 and already logged in

		}

		// Think Time
		try{Thread.sleep(getThinkTime());}
		catch(InterruptedException e){
			e.printStackTrace();
		}
		if(HTML4){
			//newPage=doSubmit2(url,data,"registeruser?");
			newPage=doSubmit3(new StringBuffer(runSettings.rootURL).append("/registeruser?"),data,client);
			if (newPage.indexOf("<HEAD><TITLE>Register User</TITLE></HEAD>")==-1){
				client.userInfo.registered=true;
				client.loggedIn=true;
			}
			else
				newPage=new StringBuffer(runSettings.rootURL).append("/registeruser");

		}
		else{
			if (success==false){
				newPage=new StringBuffer(runSettings.rootURL).append("/registeruser?").append(createURL(data));
			}
			else{
				client.userInfo.userID=new StringBuffer(node.get("userID").getValueAsText());
				newPage=new StringBuffer(runSettings.rootURL).append("/myaccount");
			}
		}
		return newPage;
	}

	/**
	 * Determines the think time before going to the next page
	 * Deducts from the think time the amount of time since the 
	 * page opened until this calculation is performed
	 * @return Think time in ms
	 */
	public int getThinkTime(){
		int thinkTime=(int)expDist(initialThinkTime);

		if (client.formFiller==false){
			for (String m:data.keySet())
				thinkTime+=(int)(data.get(m).length()/client.typingSpeed);
		}
		else{
			thinkTime+=(int)expDist(2000);
		}
		if (verbose)System.out.println("User: "+client.userInfo.username+" - Think Time: "+thinkTime+" ms");
		return (int) ((thinkTime-(new Date().getTime()-pageOpenTime))/runSettings.thinkTimeSpeedUpFactor);
	}

}
